#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll n, m;
char s[1000001];
ll cnt;
ll tr[1000001][2], ls[1000001], rs[1000001];
void add(ll x, ll y) {
//	cout<<x<<' '<<y;
	tr[x][0]++;
	if (y == m + 1) {
		tr[x][1]++;
		return;
	}
	if (s[y] == '0') {
		if (!ls[x])
			ls[x] = ++cnt;
		add(ls[x], y + 1);
	} else {
		if (!rs[x])
			rs[x] = ++cnt;
		add(rs[x], y + 1);
	}
}
ll tp[1000001];
struct comp {
	bool operator()(pair<ll, bool> a, pair<ll, bool> b) {
		return a.first > b.first;
	}
};
priority_queue<pair<ll, bool>, vector<pair<ll, bool >>, comp>q[1000001];
ll ans = 0;
void gt(ll x, ll y) {
	ans += q[x].top().first - y;
	if(q[x].top().second == 1) {
		q[x].push(make_pair(q[x].top().first + 1, 1));
		q[x].push(make_pair(q[x].top().first + 1, 1));
	}
	else
		q[x].push(make_pair(q[x].top().first + 2, 1));
	q[x].pop();
}
void solve(ll x, ll y) {
	if(ls[x] != 0) {
		solve(ls[x], y + 1);
		tp[x] = tp[ls[x]];
	}
	if(rs[x] != 0) {
		solve(rs[x], y + 1);
		if(tp[x] != 0) {
			if(q[tp[x]].size()<q[tp[rs[x]]].size())
				tp[x] = tp[rs[x]];
		}
		else tp[x] = tp[rs[x]];
	}
	if(tp[x] == 0)
		tp[x] = x;
	if(tp[x] != tp[ls[x]])
		while(q[tp[ls[x]]].size())
			q[tp[x]].push(q[tp[ls[x]]].top()), q[tp[ls[x]]].pop();
	if(tp[x] != tp[rs[x]])
		while(q[tp[rs[x]]].size())
			q[tp[x]].push(q[tp[rs[x]]].top()), q[tp[rs[x]]].pop();
	if ((ls[x] == 0) && (rs[x] == 0)) {
		tp[x] = x;
		if(tr[x][1] == 1) {
			q[x].push(make_pair(y + 2, 1));
			return;
		}
		int z = 1, r = 0;
		for(; z * 2 <= tr[x][1]; z *= 2, r++) ans += tr[x][1];
		if(z == tr[x][1]) {
			for(int i = 1; i <= tr[x][1]; i++)
				q[x].push(make_pair(y + r + 2, 1));
			return;
		}
		else {
			ans += 2 * (tr[x][1] - z);
			for(int i = 1; i <=2*(tr[x][1]-z); i++)
				q[x].push(make_pair(y + r + 3, 1));
			for(int i = 1; i <=2*z-tr[x][1]; i++)
				q[x].push(make_pair(y + r + 2, 0));
		}
		return;
	}
	else {
		if (ls[x] == 0)
			q[tp[x]].push(make_pair(y + 1, 0));
		if (rs[x] == 0)
			q[tp[x]].push(make_pair(y + 1, 0));
	}
	if(tr[x][1] == 1 && tr[x][0]>tr[x][1]) {
		for (int i = 1; i <= tr[x][1]; i++)
			gt(tp[x], y);
	} else {
		if (tr[x][1] > 1ll)
			for (int i = 1; i <= tr[x][1]; i++)
				gt(tp[x], y);
	}
}
int main() {
	freopen("id.in", "r", stdin);
	freopen("id.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n;
	cnt++;
	for (int i = 1; i <= n; i++) {
		cin >> (s + 1);
		m = strlen(s + 1);
		add(1, 1);
	}
	solve(1, 1);
	cout << ans;
	return 0;
}
